package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
	"web-tools-go/define"
)

func TimeStampPage(c *gin.Context) {
	unix := time.Now().Unix()
	dateTime := time.Now().Format(define.DataTimeLayout)
	c.HTML(http.StatusOK, "time_stamp.html", gin.H{
		"unix": unix,
		"time": dateTime,
	})
}

// TimeStampToTime 时间戳转时间
func TimeStampToTime(c *gin.Context) {
	unix, _ := strconv.Atoi(c.PostForm("unix"))
	dateTime := time.Unix(int64(unix), 0).Format(define.DataTimeLayout)
	c.JSON(http.StatusOK, gin.H{
		"time": dateTime,
	})
}

// TimeToTimeStamp 时间转时间戳
func TimeToTimeStamp(c *gin.Context) {
	dateTime := c.PostForm("time")
	ti, _ := time.Parse(define.DataTimeLayout, dateTime)
	unixTime := ti.Unix()
	c.JSON(http.StatusOK, gin.H{
		"unix": unixTime,
	})
}
